

import 'package:flutter/material.dart';
import 'package:syria_sports/data/models/error_model.dart';
import 'package:syria_sports/presentation/offers_page/cubit/offers_screen.dart';
import 'package:syria_sports/presentation/subscriptions/subscriptions_page/models/subscription.dart';

import '../../core/utils/pref_utils.dart';
import '../../core/utils/show_toast.dart';
import '../../presentation/gyms/gyms_page/models/gym.dart';
import '../apiClient/api_client.dart';
import '../apiClient/api_keys.dart';
import '../models/auth/user.dart';
import '../models/data.dart';
import '../remote/dio_helper.dart';

class SubscriptionsController{
  static Future<Data?> getAllSubscriptions({int? type})async{
    var res = await DioHelper.getData(
        url: ApiClient.getSubscriptions,
        query: {
          'type':type,
        },
        bearerToken: PrefUtils.getToken());

    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
      return null;
    }else{
      // showMessage(res.data[ApiKeys.message]);
    }
    Data response = Data.fromJson(res.data);
    return response;
  }
  static Future<Data?> getUserSubscriptions({required User user})async{
    var res = await DioHelper.getData(
        url: ApiClient.userSubscriptions,
        bearerToken: PrefUtils.getToken(),
      query: {
          'id': user.userId,
      }
    );

    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
      return null;
    }else{

    }
    Data response = Data.fromJson(res.data);
    return response;
  }
  static Future<Data?> addSubscription(Subscription subscription)async{
    var res = await DioHelper.postData(
      url: ApiClient.subscription,
      data: subscription.toJson(),
      bearerToken: PrefUtils.getToken(),
    );
    if(res == null)return null;
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else{
     showMessage(res.data[ApiKeys.message]);
    }
    return null;
  }

  static Future<Data?> getSubscriptionById(int id)async{
    var res = await DioHelper.getData(
      url: ApiClient.subscription,
      query: {
        'id':id
      },
      bearerToken: PrefUtils.getToken(),
    );
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else if(!res.data[ApiKeys.state]){
      showMessage(res.data[ApiKeys.message]);
    }else{
      Data response = Data.fromJson(res.data);
      return response;
    }
    return null;
  }

  static Future<Data?> deleteById(String id)async{
    var res = await DioHelper.postData(
      url: ApiClient.subscriptionDelete,
      data: {
        'id':id
      },
      query: {
        'id':id,
      },
      bearerToken: PrefUtils.getToken(),
    );
    if(res == null)return null;
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else{
      showMessage(res.data[ApiKeys.message]);
    }

    return null;
  }

  static Future<Data?> check({User? user})async{
    var res = await DioHelper.getData(
        url: ApiClient.checkSubscription,
        bearerToken: PrefUtils.getToken(),
        query: {
          if(user!= null)
          'id': user.userId,
        }
    );

    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
      return null;
    }else{
      Data response = Data.fromJson(res.data);
      if( !response.state){
        showErrorMessage(message: response.message);
        showOffersScreen();
      }
      return response;
    }


  }
  static Future<Data?> addAdminSubscription()async{
    var res = await DioHelper.postData(
      url: ApiClient.adminSubscription,
      data: {
        "test":null
      },
      bearerToken: PrefUtils.getToken(),
    );
    print(res);
    if(res == null)return null;
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else{
      showMessage(res.data[ApiKeys.message]);
      return Data.fromJson(res.data);
    }
    return null;

  }



}